/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System;
using System.Runtime.Serialization;

namespace SurveillanceDotNet.Model.RegularActions
{
    [Serializable]
    public class SavePictureRegularAction : RegularAction
    {
        public string DirectoryPath { get; set; }

        public SavePictureRegularAction(string directoryPath)
            : base()
        {
            DirectoryPath = directoryPath;

            if (!Directory.Exists(DirectoryPath)) Directory.CreateDirectory(DirectoryPath);
        }

        protected override void Action()
        {
            for (int i = 0; i < _Database.Cameras.Count; i++)
            {
                string fileName = Path.Combine(DirectoryPath, "camera_" + i + "_" + DateTime.Now.ToString("ddMMyyyy_hhmmss") + ".jpeg");

                if (File.Exists(fileName)) File.Delete(fileName);

                Image bmp = _Database.Cameras[i].CurrentFrame;
                bmp.Save(fileName, ImageFormat.Jpeg);
            }
        }

        #region ISerializable Implementation
        protected SavePictureRegularAction(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            DirectoryPath = info.GetString("DirectoryPath");

            Status = "Not active";
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("DirectoryPath", DirectoryPath);
        }
        #endregion
    }
}
